package com.huafeng.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huafeng.commonutil.CheckUtils;
import com.huafeng.mapper.SysOperLogMapper;
import com.huafeng.model.base.Result;
import com.huafeng.model.entity.SysOperLog;
import com.huafeng.model.vo.SysOperLogQueryVo;
import com.huafeng.model.vo.SysOperLogVo;
import com.huafeng.service.SysOperLogService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 花风
 * @date 2023/8/18 18:21
 * @Description SysOperLogServiceImpl
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements SysOperLogService {
    @Override
    public Result queryOperLogByPage(SysOperLogQueryVo sysOperLogQueryVo) {
        Page<SysOperLog> sysOperLogPage = new Page<>(sysOperLogQueryVo.getPage(), sysOperLogQueryVo.getLimit());
        // 获取参数
        String operName = sysOperLogQueryVo.getOperName();
        String title = sysOperLogQueryVo.getTitle();
        String createTimeBegin = sysOperLogQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysOperLogQueryVo.getCreateTimeEnd();

        // 创建查询对象
        QueryWrapper<SysOperLog> queryWrapper = new QueryWrapper<>();
        Map<String,String> params = new HashMap<>();
        params.put("oper_name",operName);
        params.put("title",title);
        params.put("createTimeBegin",createTimeBegin);
        params.put("createTimeEnd",createTimeEnd);
        queryWrapper =CheckUtils.checkParams(queryWrapper, sysOperLogQueryVo,params);

        // 分页条件查询
        sysOperLogPage = page(sysOperLogPage, queryWrapper);
        sysOperLogPage.getRecords().forEach(sysOperLog -> {
            if (sysOperLog.getStatus() == 1){
                sysOperLog.getParams().put("flag","正常");
            }else {
                sysOperLog.getParams().put("flag","异常");
            }
        });
        return Result.ok(sysOperLogPage);
    }

    @Override
    public void export(HttpServletResponse response) {
        try {
            List<SysOperLog> sysOperLogs = this.query().orderByDesc("create_time").last("limit 100").list();
            // 将sysOperLog转换为Excel导出的实体类
            List<SysOperLogVo> sysOperLogVos = sysOperLogs.stream().map(sysOperLog -> {
                SysOperLogVo sysOperLogVo = new SysOperLogVo();
                BeanUtils.copyProperties(sysOperLog, sysOperLogVo);
                return sysOperLogVo;
            }).collect(Collectors.toList());
            // 指定导出的格式
            ExportParams exportParams = new ExportParams("操作日志", "管理员", ExcelType.XSSF);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, SysOperLogVo.class, sysOperLogVos);

            // 导出日志
            String filename = "操作日志.xlsx";
            ServletOutputStream outputStream = response.getOutputStream();

            // 设置响应信息
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(),StandardCharsets.ISO_8859_1));
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            workbook.write(outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
